layer = layui.layer
var userinfo = new Vue({
    el: "#app",
    data: {
        oldpwd: '',
        oldpwd_check: false,
        oldpwd_eshow: false,
        newpwd: '',
        newpwd_check: false,
        newpwd_eshow: false,
        verpwd: '',
        verpwd_check: false,
        verpwd_eshow: false,
        emailcode: '',
        emailcodebtn: '获取验证码',
        ebtndisabled: false,
        ebtnclass: 'layui-btn layui-btn-sm layui-btn-normal',
        originbtn: 'layui-btn layui-btn-sm layui-btn-normal',
        valid_emailcode: ''
    },
    methods: {
        emailcodeact: function() {
            that = this
            this.ebtndisabled = true
            this.ebtnclass = this.originbtn + ' layui-btn-disabled'
            cnt = 5
            var timer = setInterval(function() {
                if (cnt != 0) {
                    that.emailcodebtn = cnt + "s后再获取"
                    cnt--
                } else {
                    that.ebtndisabled = false
                    that.ebtnclass = that.originbtn
                    that.emailcodebtn = "获取验证码"
                    clearInterval(timer)
                }
            }, 1000);
            let param = new URLSearchParams()
            axios.get("http://127.0.0.1:8000/validcode/?type=email").then(
                function(response) {
                    var resp = response.data
                    layer.msg('验证码已发送到:' + $.cookie('email'), { time: 3000 });
                    console.log(resp)
                    code = resp['code']
                    if (code) {
                        that.valid_emailcode = code
                    } else {
                        layer.alert(err, {
                            icon: 2,
                            skin: 'layer-ext-demo' //见：扩展说明
                        })
                    }
                },
                function(err) {
                    console.log(err)
                    layer.alert('十分抱歉，服务端验证码发送失败', {
                        icon: 2,
                        skin: 'layer-ext-demo' //见：扩展说明
                    })
                }
            )
        },
        pwdformatoldact: function() {
            const regPassword = /^[\W\da-zA-Z_]{6,20}$/
            if (this.oldpwd.length > 0) {
                this.oldpwd_eshow = true
                if (regPassword.test(this.oldpwd)) {
                    this.oldpwd_check = true
                } else {
                    this.oldpwd_check = false
                }
            } else {
                this.oldpwd_eshow = false
                this.oldpwd_check = false
            }
        },
        pwdformatnewact: function() {
            const regPassword = /^[\W\da-zA-Z_]{6,20}$/
            if (this.newpwd.length > 0) {
                this.pwdformatveract()
                this.newpwd_eshow = true
                if (regPassword.test(this.newpwd)) {
                    this.newpwd_check = true
                } else {
                    this.newpwd_check = false
                }
            } else {
                this.newpwd_eshow = false
                this.newpwd_check = false
            }
        },
        pwdformatveract: function() {
            if (this.verpwd.length > 0) {
                this.verpwd_eshow = true
                if (this.newpwd == this.verpwd) {
                    this.verpwd_check = true
                } else {
                    this.verpwd_check = false
                }
            } else {
                this.verpwd_eshow = false
                this.verpwd_check = false
            }

        },
        submit: function() {
            that = this
            if (!this.oldpwd_check) return layer.alert('旧密码格式错误', { icon: 2, skin: 'layer-ext-demo' })
            if (!this.newpwd_check) return layer.alert('新密码格式错误', { icon: 2, skin: 'layer-ext-demo' })
            if (!this.verpwd_check) return layer.alert('确认密码格式错误', { icon: 2, skin: 'layer-ext-demo' })
            console.log(this.valid_emailcode, this.emailcode, this.emailcode == this.valid_emailcode)
            if (!this.valid_emailcode || this.valid_emailcode != this.emailcode) return layer.alert('验证码错误，请检查验证码', { icon: 2, skin: 'layer-ext-demo' })
            this.valid_emailcode = ''
            let param = new URLSearchParams()
            param.append('oldpwd', this.oldpwd)
            param.append('newpwd', this.newpwd)
            axios.post("http://127.0.0.1:8000/alterpassword/", param).then(
                function(response) {
                    var resp = response.data
                    console.log(resp)
                    msg = resp['msg']
                    if (resp['response']) {
                        layer.alert(msg && msg || '修改成功', {
                            icon: 1,
                            skin: 'layer-ext-demo' //见：扩展说明
                        })
                    } else {
                        layer.alert(msg && msg || '修改失败', {
                            icon: 2,
                            skin: 'layer-ext-demo' //见：扩展说明
                        })
                    }
                },
                function(err) {
                    console.log(err)
                    layer.alert('修改失败', {
                        icon: 2,
                        skin: 'layer-ext-demo' //见：扩展说明
                    })
                }
            )
        },

    }
})